Repository navigation
Image Editor: Remove floating-point noise from the edit read-back - #87376
Conversation
…tputSize Working the edit back out of the state could put a crop edge at 7e-8 instead of 0, and give a size of 1439.9999999999998 pixels instead of 1440. A value within a millionth of a whole number is now returned as that number.
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +94 B (0%) Total Size: 8.71 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
|
andrewserong
left a comment
There was a problem hiding this comment.
LGTM, nice follow-up!
| * pixels. A millionth, of the image or of a pixel, is far below anything | ||
| * a person can set, so a value that close to a whole number is that number. | ||
| */ | ||
| const WHOLE_NUMBER_TOLERANCE = 1e-6; |
There was a problem hiding this comment.
We're using 1e-6 in a few places. Not for now, but if we could potentially consolidate them if we think it's a universal tolerance we want to use? E.g. there's also:
Not a big deal to have them separate if you like the clarity of differently named constants, though!
There was a problem hiding this comment.
Oh yeah, it's in a few places. I guess it's common enough to warrant centralization. Thanks for spotting. I'll do a follow up if I can come up with a decent enough const name 😄
| * pixels. A millionth, of the image or of a pixel, is far below anything | ||
| * a person can set, so a value that close to a whole number is that number. | ||
| */ | ||
| const WHOLE_NUMBER_TOLERANCE = 1e-6; |
There was a problem hiding this comment.
Oh yeah, it's in a few places. I guess it's common enough to warrant centralization. Thanks for spotting. I'll do a follow up if I can come up with a decent enough const name 😄
So the shared value is more of a coincidence I feel. There are six! But they apply to different things like degrees, pixels, zoom etc. A single constant could mean that they all share the same boundaries, and would make it harder to customize them later. I'll leave for now but keep it mind. Thanks again! |
All good! Thanks for looking into it, and yeah I can imagine us eventually wanting different levels of granularity, good to keep it as is for now 👍 |
What?
Follow-up to #86665.
getImageEditandgetOutputSizereturn a value that is within a millionth of a whole number as that number.A crop fraction within a millionth of 0 or 1 reads as 0 or 1, and a dimension within a millionth of a pixel of a whole number reads as that pixel count.
Why?
Working the edit back out of the editor state goes through the pan and zoom, which leaves floating-point noise.
On a 1600 x 900 image, a crop of nine tenths read back as
1439.9999999999998by809.9999999999999pixels, a crop on the left edge started aty: 2.2992026060819626e-9, and a quarter turn gave a width of900.0000000000001.Anything that shows or compares these values has to clean them up first.
Example: an agent driving
applyImageEditover MCP spent words on every one of them.Rotation is not touched: it is the number the person set, and no run showed noise in it.
Testing Instructions
npm run build.0for the edge of the second crop:On trunk:
The same command on trunk prints
1439.9999999999998,809.9999999999999,"y":2.2992026060819626e-9and900.0000000000001.The crop fractions of the first edit stay as they are:
0.05000000000000002is not near a whole number, and the size is what the snap is for.